package com.turtle3d.user.visualizators;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix4f;

import com.turtle3d.ResourceManager;
import com.turtle3d.formallanguage.VisualSymbol;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.visualizator.MyCone;
import com.turtle3d.visualizator.VisualData;
import com.turtle3d.visualizator.Visualizator;

public class Fern3DLeafs extends Visualizator {

	/**
	 * @param args
	 */

	public BranchGroup visualize(VisualData visualData) {
		BranchGroup branchGroup = new BranchGroup();

		ArrayList<VisualSymbol> visualSequence = visualData.getVisualSequence();

		for (int i = 0; i < visualSequence.size(); i++) {

			VisualSymbol visualSymbol = visualSequence.get(i);

			if (visualSymbol.getSymbol().equals(new Symbol("F"))) {

				int depth = visualSymbol.getDepth();
				Float d = visualSymbol.getSymbol().getParameter("distance");
				//float normalizedDepth = depth	/ (visualData.getMaximalDepthReached() * 1f);
				Float r = visualSymbol.getSymbol().getParameter("radius");

				Shape3D myCone;
				//if (visualSymbol.getSymbol().getBirthIteration() < 3);
				myCone = new MyCone(d, r, 0.9f * r, visualSymbol.getSymbol().getBirthIteration()==0?8:3);
				// myCone = ResourceManager.getShape3D("objects/element.obj");
				Appearance appearance = new Appearance();
				Material m = new Material();
				m.setDiffuseColor(new Color3f(0f, 1f, 0f));
				appearance.setMaterial(m);
				myCone.setAppearance(appearance);

				myCone.setName("Grammar_" + depth);

				branchGroup.addChild(createTransformGroup(visualSymbol
						.getTransform(), myCone));
			}

			if (visualSymbol.getSymbol().equals(new Symbol("B"))) {
				Shape3D leaf = visualSymbol.getSymbol().getBirthIteration()<=1?ResourceManager.getShape3D("leaf"):ResourceManager.getShape3D("lo_leaf");							
				
				Matrix4f scale = new Matrix4f();
				scale.setIdentity();
																
				scale.setScale(visualSymbol.getSymbol().getParameter("leafSize"));
				Matrix4f transform = visualSymbol.getTransform();
				transform.mul(scale);																	

				branchGroup.addChild(createTransformGroup(transform,leaf));
			}

		}

		return branchGroup;
	}

}
